package com.gxwy.test.szsm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.gxwy.shenzhou.HeaderRequest;
import com.gxwy.shenzhou.KaiHuMessageHandler;
import com.gxwy.shenzhou.KaihuRequest;
import com.gxwy.shenzhou.TCPClient;

public class Simulator extends JFrame {
	private static final long serialVersionUID = 1L;
	private JLabel helloJLabel;
	private JTextField inputJTextField;
	private JLabel helloJLabel2;
	private JTextField inputJTextField2;
	private JButton closeJButton;
	private JButton sendJButton;

	public Simulator() {
		Container contentpane = getContentPane(); // 初始化一个容器
		setTitle("模拟器窗口");// 设置标题
		setSize(600, 400);// 设置大小
		contentpane.setBackground(Color.GRAY); // 设置颜色
		JPanel panel=new JPanel();
        JPanel panel_01=new JPanel();
        panel_01.setBackground(Color.YELLOW);
        panel_01.setLayout(new GridLayout(1,2));
        
        JPanel panel_02=new JPanel();
        panel_02.setBackground(Color.PINK);
        panel_02.setLayout(new GridLayout(1,2));
        
        JPanel panel_03=new JPanel();
        panel_03.setBackground(Color.BLUE);
        panel_03.setLayout(new GridLayout(1,2));
        
        panel.setLayout(new GridLayout(3,1));
        
        panel.add(panel_01);
        panel.add(panel_02);
        panel.add(panel_03);
        contentpane.add(panel);
		//contentpane.setLayout(null);
        
        //-----------------------------------------------------------------
		// 设置容器布局管理器为无
		helloJLabel2 = new JLabel();
		helloJLabel2.setText("Hello World!");// 设置文本
		helloJLabel2.setSize(100, 20);// 设置大小
		//helloJLabel.setLocation(250, 150);// 设置位置
		panel_01.add(helloJLabel2);// 把helloJLabel加入到容器中
		
		inputJTextField2 = new JTextField();
		//inputJTextField.setBounds(150, 150, 80, 20);
		inputJTextField2.setText("12334");
		inputJTextField2.setSize(100, 20);
		panel_01.add(inputJTextField2);
		
		//-----------------------------------------------------------------
		helloJLabel = new JLabel();
		helloJLabel.setText("Hello World!");// 设置文本
		helloJLabel.setSize(100, 20);// 设置大小
		//helloJLabel.setLocation(250, 150);// 设置位置
		panel_02.add(helloJLabel);// 把helloJLabel加入到容器中		
		
		inputJTextField = new JTextField();
		//inputJTextField.setBounds(150, 150, 80, 20);
		inputJTextField.setText("12334");
		inputJTextField.setSize(100, 20);
		panel_02.add(inputJTextField);
		
		//-----------------------------------------------------------------
		sendJButton = new JButton("发送");
		//outputJButton.setBounds(150, 200, 80, 20);
		panel_03.add(sendJButton);
		sendJButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendJButtonActionPerformed(e);
			}
		});
		closeJButton = new JButton("关闭");// 设置文本，和上面的方法不同
		//closeJButton.setBounds(250, 200, 70, 20);// 设置位置和大小，和上面方法不同
		panel_03.add(closeJButton);// 把closeJButton加入到容器中
		
		closeJButton.addActionListener(// 添加按钮监听
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						closeJButtonActionPerformed(e);// 调用方法
					}
				});

		setVisible(true);// 使窗口可显示
	}

	protected void sendJButtonActionPerformed(ActionEvent e) {
		//helloJLabel.setText(inputJTextField.getText());
		//helloJLabel2.setText(inputJTextField2.getText());
		// 得到 inputJTextField并在helloJLabel中显示出来
		KaiHuMessageHandler ksm=new KaiHuMessageHandler();
		HeaderRequest hr=new HeaderRequest(false);
		String str = null;
		try {
			hr.setTuxjydm("4685");
			hr.setGgjydm("1000");
			KaihuRequest kr=new KaihuRequest();
			kr.setHeader(hr);
			kr.setZhdh("8001011000000034778");
			kr.setMm("123456");
			kr.setKhmc("cuicui");
			kr.setYddh("18607176741");
			kr.setZz_ed("5000");
			kr.setDz("北京市海淀区上地西里颂芳园");
			str=kr.build();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		TCPClient tcpclient;
		try {
			tcpclient = new TCPClient("192.168.3.129",10001,ksm);
			tcpclient.sendMessage(str);
		} catch (UnknownHostException e2) {
			e2.printStackTrace();
			return;
		} catch (IOException e3) {
			e3.printStackTrace();
			return;
		}
	}

	private void closeJButtonActionPerformed(ActionEvent e) {// 按钮触发使用的方法
		dispose();// 关闭这个窗口
	}

	public static void main(String[] args) {
		Simulator application = new Simulator();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口的关闭方式为使用
																	// System
																	// exit
																	// 方法退出应用程序。仅在应用程序中使用。
		
		
	}
}